package com.example.dllo.baidumusic.fragment;
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑       永无BUG
*/

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.example.dllo.baidumusic.R;
import com.example.dllo.baidumusic.adapter.OnitenClickListener;
import com.example.dllo.baidumusic.adapter.SingDanAdapter;
import com.example.dllo.baidumusic.adapter.LunboViewpagerAdapter;
import com.example.dllo.baidumusic.bean.LunBopicBean;
import com.example.dllo.baidumusic.bean.SingdanBean;
import com.example.dllo.baidumusic.nettool.URLValuas;
import com.example.dllo.baidumusic.main.NewActivity;
import com.example.dllo.baidumusic.nettool.BaseFragment;
import com.example.dllo.baidumusic.nettool.NetTool;
import com.example.dllo.baidumusic.events.UpUrlEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Timer;


/**
 * Created by dllo on 16/8/15.
 */
public class RecommendFragment extends BaseFragment implements View.OnClickListener {

    //当前索引位置以及上一个索引位置
    private static int index = 0, preIndex = 0;
    //定时器，用于实现轮播
    private Timer timer = new Timer();
    private Button btnallSinger;
    private ViewPager lbviewPager;
    private Handler handler;
    private ArrayList<String> strings;
    private RecyclerView rV;
    private SingDanAdapter sadapter;
    private GridLayoutManager manager;

    @Override
    protected int setLayout() {
        return R.layout.fragment_recommend;
    }

    @Override
    protected void initView() {
        btnallSinger = bindView(R.id.recommend_fragment_allsinger);
        btnallSinger.setOnClickListener(this);


        lbviewPager = bindView(R.id.recommend_fragment_viewpaget);
        lbviewPager.setOnClickListener(this);
        rV = bindView(R.id.recommend_fragment_recyview);
        manager = new GridLayoutManager(getContext(), 3);
        sadapter = new SingDanAdapter(getContext());
        rV.setLayoutManager(manager);

    }


    @Override
    protected void initData() {


        NetTool netTool = new NetTool();

        netTool.getNetData(URLValuas.RECOMMAND_HOT, SingdanBean.class, new NetTool.NetListener<SingdanBean>() {
            @Override
            public void onSuccess(SingdanBean singdanBean) {
                sadapter.setArrayList(singdanBean);
                rV.setAdapter(sadapter);
                UpData(singdanBean);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
//轮播图
        final LunboViewpagerAdapter lunboAdapteradapter = new LunboViewpagerAdapter(getContext());
        strings = new ArrayList<>();
        netTool.getNetData(URLValuas.RECOMMAND_CAROUSE, LunBopicBean.class, new NetTool.NetListener<LunBopicBean>() {
            @Override
            public void onSuccess(LunBopicBean lunBopicBean) {
                for (int i = 0; i < lunBopicBean.getPic().size(); i++) {

                    strings.add(lunBopicBean.getPic().get(i).getRandpic());
                }
                lunboAdapteradapter.setStrings(strings);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });

        lunboAdapteradapter.setViewPager(lbviewPager);
        lbviewPager.setAdapter(lunboAdapteradapter);
        lbviewPager.setCurrentItem(3000, false);


        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(4000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    handler.sendEmptyMessage(lbviewPager.getCurrentItem());
                }
            }
        }).start();
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                int current = msg.what;
                lbviewPager.setCurrentItem(current + 1);
                return false;
            }
        });
    }

    private void UpData(final SingdanBean singdanBean) {
        sadapter.setOnitenClickListener(new OnitenClickListener() {
            @Override
            public void onClick(int itemPostion) {
                String url = singdanBean.getContent().getList().get(itemPostion).getListid();
                String aUrl = URLValuas.SONGLIST_DETAIL_Front + url + URLValuas.SONGLIST_DETAIL_BEHIND;
                UpUrlEvent upUrlEvent = new UpUrlEvent();
                upUrlEvent.setURL(aUrl);
                EventBus.getDefault().post(upUrlEvent);
                SingDanListFragment singDanListFragment = new SingDanListFragment();
                Bundle bundle = new Bundle();
                bundle.putString("XXX", aUrl);
                singDanListFragment.setArguments(bundle);
                NewActivity.getManager(singDanListFragment);
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.recommend_fragment_allsinger:
                NewActivity.getManager(new AllSingerFragment());
                break;
            case R.id.recommend_fragment_viewpaget:
                NewActivity.getManager(new LunBoFragment());
        }
    }


}
